package pm;

import pm.config.Configuration;
import pm.core.Core;
import pm.gui.GuiModuleImpl;
import pm.devices.DevicesModuleImpl;
import pm.utils.ClassPathHacker;
import pm.oo.OOModuleImpl;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * Created by fat3kb
 * Date: 03.02.2008
 * Time: 23:24:11
 */
public class PM extends Core {
    private static final String DEFAULT_CFG_PATH = "pm.xml";
    public static final boolean DEBUG = false;

    /**
     * Makes initialization and configures modules
     * according <code>Configuration</code> object and starts
     * application.
     *
     * @param configuration
     * @throws InterruptedException
     */
    public void launch(Configuration configuration) {
        this.configuration = configuration;

        try {
            ClassPathHacker.addFile(this.configuration.getOoHome() + "\\program\\classes\\juh.jar");
            ClassPathHacker.addFile(this.configuration.getOoHome() + "\\program\\classes\\jurt.jar");
            ClassPathHacker.addFile(this.configuration.getOoHome() + "\\program\\classes\\ridl.jar");
            ClassPathHacker.addFile(this.configuration.getOoHome() + "\\program\\classes\\unoil.jar");
        } catch (IOException e) {
            e.printStackTrace();
        }

        this.guiModule = new GuiModuleImpl(this, this.configuration);
        this.guiModule.showSplashScreen();

        this.balans = new BigDecimal(0);

        // Configure devices update thread
        this.devicesModule = new DevicesModuleImpl(this, this.configuration);
        this.guiModule.setDrives(this.devicesModule.getDrives());

        this.ooModule = new OOModuleImpl();
        this.ooModule.createConnection();

        this.guiModule.showMainScreen();
    }

    public static void main(String[] args) {
        try {
            PM pm = new PM();
            String cfgPath = args.length > 0 ? args[0] : DEFAULT_CFG_PATH;
            pm.launch(Configuration.load(cfgPath));
        } catch (Exception e) {
            e.printStackTrace();
		}
	}
}
